package com.accompany.fragment.my.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.accompany.R;
import com.accompany.api.bean.AountMoneyBean;
import com.accompany.api.bean.ExteactMoneyBean;
import com.accompany.api.category.ExtractMoneyCategory;
import com.accompany.api.presenter.ExtractMoneyPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.dialog.LoginOutDialog;
import com.accompany.dialog.RoundCornerDialog;
import com.accompany.listener.OnQuiteDialogClickListener;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;
import retrofit2.http.Query;

/**
 * 信息说明: 立即提现界面
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/30 14 ：54
 * 项目名称：yuanban
 */
public class ExtractMoneyActivity extends BaseActivity implements ExtractMoneyCategory.View {
    @BindView(R.id.bankCard)
    TextView mBankCard;
    @BindView(R.id.edit_money)
    EditText mEditMoney;
    @BindView(R.id.check_box_jin)
    CheckBox mCheckBox;
    @BindView(R.id.linear_areed_jin)
    LinearLayout mLlAgreement;
    @BindView(R.id.zhanghu_yue)
    TextView mPrice;
    @BindView(R.id.remark)
    TextView remark;
    @BindView(R.id.remark_msg)
    TextView mRemarkMSG;
    private String mBankMsg;
    private String bankAddress;
    private String mBankCardNumber;
    private String mToken;
    private long mUserId;
    private ExtractMoneyPresenter mPrsenter;
    private double mAountPrice;
    private double mlimitPrice;
    private AountMoneyBean bean;
    private RoundCornerDialog loginOutDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_extract_money;
    }

    @Override
    public void initLayoutView() {
        YCAppBar.translucentStatusBar(this, true);
        mToken = (String) SPUtils.get(ExtractMoneyActivity.this, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = ExtractMoneyActivity.this.getSharedPreferences("First", MODE_PRIVATE);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        mPrsenter = new ExtractMoneyPresenter(this);
        SetPersenter(mPrsenter);
        mPrsenter.getMoneyPrice(mToken, mUserId);

        mEditMoney.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().startsWith("0") ) {
                    // 不能输入0开头的数字
                    s.clear();
                }
            }
        });

    }

    @OnClick({R.id.to_card, R.id.base_back, R.id.to_tixian,R.id.tixian_jilu, R.id.quanbu_tixian})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.tixian_jilu:
                openActivityWitchAnimation(ExtractMoneyRecordActivity.class);
                break;
            case R.id.quanbu_tixian:
                mEditMoney.setText(String.valueOf(mAountPrice));
                break;
            case R.id.to_card:
                Intent intent = new Intent(ExtractMoneyActivity.this, MyCardActivity.class);
                intent.putExtra("states", "0");
                startActivityForResult(intent, 100);
                break;
            case R.id.to_tixian:
                String money = mEditMoney.getText().toString().trim();
                String bankCard = mBankCard.getText().toString().trim();
                if (money.equals("")) {
                    ToastUtil.showToastINFO(ExtractMoneyActivity.this, "请输入提现金额");
                    return;
                }
                if (Double.parseDouble(money) < mlimitPrice) {
                    ToastUtil.showToastINFO(ExtractMoneyActivity.this, "提现金额不能小于最小金额" + mlimitPrice);
                    mEditMoney.setText("");
                    return;
                }
                if (Double.parseDouble(money) > mAountPrice) {
                    ToastUtil.showToastINFO(ExtractMoneyActivity.this, "提现金额不能大于最大金额" + mAountPrice);
                    mEditMoney.setText("");
                    return;
                }
                if (TextUtils.isEmpty(mBankCardNumber)) {
                    ToastUtil.showToastINFO(ExtractMoneyActivity.this, "请选择提现银行卡");
                    return;
                }
                if (mCheckBox.isChecked() == false) {
                    mLlAgreement.startAnimation(AnimationUtils.loadAnimation(this, R.anim.transla_checkbox));
                    return;
                }

                    JSONObject result = new JSONObject();
                    try {
                        result.put("acdId", mUserId);
                        result.put("extractType", "bank");
                        result.put("bankCode", mBankCardNumber);
                        result.put("bankAddress", bankAddress);
                        result.put("extractPrice", money);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    mPrsenter.getBankCardlist(mToken, result.toString());


                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            mBankMsg = data.getStringExtra("bank");
            bankAddress = data.getStringExtra("bankAddress");
            mBankCardNumber = data.getStringExtra("bankCard");
            mBankCard.setText(data.getStringExtra("bank").replace("中国","") + "(" + data.getStringExtra("bankCard").substring(data.getStringExtra("bankCard").length()-4,data.getStringExtra("bankCard").length()) + ")");
        }
    }

    @Override
    public void getMoneyPrice(AountMoneyBean bean) {
        this.bean = bean;
        if (bean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (bean.getData() != null) {
                mAountPrice = bean.getData().getAccount();
                mlimitPrice = Double.parseDouble(bean.getData().getLimitFee());
                mPrice.setText(String.valueOf(bean.getData().getAccount()));
                remark.setText(bean.getData().getLimitRemark());
                mRemarkMSG.setText(bean.getData().getRemark());
            } else {
                ToastUtil.showToastINFO(ExtractMoneyActivity.this, bean.getMsg());
            }
        }
    }

    @Override
    public void getBankCardlist(ExteactMoneyBean addCardBean) {
        if (addCardBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            ToastUtil.showToastSUCCESS(ExtractMoneyActivity.this, "提现申请成功");
            mPrice.setText("￥"+addCardBean.getData().getAccount());
            mPrice.setText("￥"+(bean.getData().getAccount()- Double.valueOf(mEditMoney.getText().toString()) ));
            mEditMoney.setText("");
            if (loginOutDialog == null){
                loginOutDialog = new RoundCornerDialog(ExtractMoneyActivity.this, "提现申请成功,立即返回",
                        "确定", "取消", new OnQuiteDialogClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        loginOutDialog.dismiss();
                        finish();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        loginOutDialog.dismiss();
                    }
                });
                loginOutDialog.show();
            }

        } else {
            ToastUtil.showToastINFO(ExtractMoneyActivity.this, addCardBean.getMsg());
        }

}
    @Override
    public void SetPersenter(Object persenter) {
    }
    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(ExtractMoneyActivity.this,mErrorMsg);
    }
}
